<?php

require_once dirname(dirname(dirname(dirname(__FILE__)))) . '/shared/db_connection.php';
include_once dirname(dirname(__FILE__)) . '/dto/article_dto.php';

function load_article_list() {
    $con = open_connect();
    //run the store proc
    $result = mysqli_query($con, "CALL GetAllJournals()")
            or die("Query fail: " . mysqli_error());

    $list = array();
    $count = 0;
    while ($row = mysqli_fetch_array($result)) {
        $item = array();
        $item["JournalID"] = $row['JournalID'];
        $item["Title"] = $row['Title'];
        $item["Thumb"] = $row['Thumb'];
        $item["ShortDescription"] = $row['ShortDescription'];
        $item["PrintISSN"] = $row['PrintISSN'];
        $item["OnlineISSN"] = $row['OnlineISSN'];
        $item["Publish"] = $row['Publish'];
        $item["Photo"] = $row['Photo'];
        $item["CreationDate"] = $row['CreationDate'];
        $list[$count] = $item;
        $count = $count + 1;
    }

    close_connect($con);
    return $list;
}

function add_article(article $article) {
    $con = open_connect();

    $query = "CALL addandUpdateJournalSubDetail(" . $article->JournalsSubID . ",'" . $article->Description . "'," . $article->JournalID . "," . $article->CategoryID . ")";
    mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

    close_connect($con);
    return 1;
}

function edit_article(article $article) {
    $con = open_connect();

    $query = "CALL addandUpdateJournalSubDetail(" . $article->JournalsSubID . ",'" . $article->Description . "'," . $article->JournalID . "," . $article->CategoryID . ")";
    mysqli_query($con, $query) or die('Query failed: ' . mysqli_error());

    close_connect($con);
    return 1;
}

function delete_article($id) {
    $con = open_connect();

    $query = 'DELETE FROM tblJournalsSubDetail WHERE JournalsSubID = ' . $id;
    mysqli_query($con, $query) or die('Query failed: ' . mysqli_error());

    close_connect($con);
}

function get_all_journal_sub_detail() {
    $con = open_connect();
    //run the store proc
    $result = mysqli_query($con, "CALL GetAllJournalSubDetail()")
            or die("Query fail: " . mysqli_error());

    $list = array();
    $count = 0;
    while ($row = mysqli_fetch_array($result)) {
        $item = array();
        $item["JournalsSubID"] = $row['JournalsSubID'];
        $item["Description"] = $row['Description'];
        $item["JournalID"] = $row['JournalID'];
        $item["CategoryID"] = $row['CategoryID'];
        $item["Title"] = $row['Title'];
        $item["catTitle"] = $row['catTitle'];
        $list[$count] = $item;
        $count = $count + 1;
    }
    close_connect($con);
    return $list;
}

function get_journal_sub_detail_by_id($id) {
    $con = open_connect();

    $query = "CALL GetJournalSubDetailById(" . $id . ")";
    $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());
    $row = mysqli_fetch_array($result);
    $item = array(
        "JournalsSubID" => $row['JournalsSubID'],
        "Description" => $row["Description"],
        "JournalID" => $row["JournalID"],
        "CategoryID" => $row["CategoryID"],
        "Title" => $row["Title"],
        "catTitle" => $row["catTitle"],
    );
    close_connect($con);
    
    return $item;
}

//Get total of journal sub detail record
function get_total_row() {
    $list = get_all_journal_sub_detail();
    return count($list);
}

//Get total page
function get_total_page($page_size) {
    $total_row = get_total_row();
    $total_page = $total_row / $page_size;
    return ($total_page - 1);
}

//Get list of current page
function get_current_page_list($current_page, $page_size) {
    $skip = $current_page * $page_size;
    $list = get_all_journal_sub_detail();
    asort($list);
    $result = array_slice($list, $skip, $page_size);
    return $result;
}
